package com.mb.app.controller;


import com.mb.app.domain.TbProductClass;
import com.mb.app.domain.TbProductInfo;
import com.mb.app.service.ITbProductClassService;
import com.mb.app.service.ITbProductInfoService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwl
 * @program
 * @description 产品管理
 * @date 2019-03-22 13:01
 **/
@Controller
@RequestMapping("/app/product")
@Slf4j
public class ProductInfoController extends BaseController {

    @Autowired
    private ITbProductClassService productClassService;
    
    @Autowired
    private ITbProductInfoService productInfoService;

    @GetMapping("/index")
    String index(Model model){
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbProductClass> productClasses = productClassService.getProductClassInfoList(map);
        model.addAttribute("productClasses",productClasses);
        return "/app/product/product_list";
    }

    @GetMapping("/selectIndex")
    String selectIndex(Model model){
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbProductClass> productClasses = productClassService.getProductClassInfoList(map);
        model.addAttribute("productClasses",productClasses);
        return "/app/product/selectProduct_list";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbProductInfo> list = productInfoService.queryList(query);
        int total = productInfoService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(Model model){
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbProductClass> productClasses = productClassService.getProductClassInfoList(map);
        model.addAttribute("productClasses",productClasses);
        return "/app/product/product_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbProductInfo TbProductInfo, HttpServletRequest request){
        TbProductInfo.setCreateUserId(getUserId());
        int result = productInfoService.save(TbProductInfo);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("productId") Long productId, HttpServletRequest request){
        Long delUserId = getUserId();
        int result = productInfoService.remove(productId,delUserId);
        return R.ok();
    }

    @GetMapping("/dtl/{productId}")
    String dtl(@PathVariable("productId") Long productId, Model model){
        model.addAttribute("productInfo",productInfoService.getDtl(productId));
        return "/app/product/product_info";
    }


    @GetMapping("/edit/{productId}")
    String edit(@PathVariable("productId") Long productId, Model model){
        model.addAttribute("productInfo",productInfoService.getDtl(productId));
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbProductClass> productClasses = productClassService.getProductClassInfoList(map);
        model.addAttribute("productClasses",productClasses);
        return "/app/product/product_edit";
    }

    @ResponseBody
    @PostMapping("/update")
    public R update(TbProductInfo TbProductInfo, HttpServletRequest request){
        TbProductInfo.setUpdateUserId(getUserId());
        int result = productInfoService.update(TbProductInfo);
        return R.ok();
    }

    @GetMapping("/copy/{productId}")
    String copy(@PathVariable("productId") Long productId, Model model){
        model.addAttribute("productInfo",productInfoService.getDtl(productId));
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbProductClass> productClasses = productClassService.getProductClassInfoList(map);
        model.addAttribute("productClasses",productClasses);
        return "/app/product/product_copy";
    }

    @ResponseBody
    @PostMapping("/copyInsert")
    public R copyInsert(TbProductInfo TbProductInfo, HttpServletRequest request){
        TbProductInfo.setUpdateUserId(getUserId());
        int result = productInfoService.copy(TbProductInfo);
        if (result > 0){
            return R.ok();
        }else {
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("code",1);
            map.put("msg","该分类下已存在该产品,请修改产品名称");
            return R.ok(map);
        }
    }

    /**
     *
     */
    @PostMapping("/productInfoNameExit")
    @ResponseBody
    boolean productInfoNameExit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !productInfoService.productInfoNameExit(params);
    }

    @ResponseBody
    @PostMapping("/status")
    public R remove(@RequestParam("productId") Long productId,@RequestParam("status") Integer status, HttpServletRequest request){
        int result = productInfoService.status(productId,status);
        return R.ok();
    }







}
